package com.lanou3g.hw0419;

import java.io.*;

/**
 * Created by dllo on 18/4/20.
 */
public class DelThread extends Thread {
    private File file;

    public DelThread(File file) {
        this.file = file;
    }

    @Override
    public void run() {
        //获取当前文件所在文件夹
        File pathDir = file.getParentFile();

        String newName = file.getName().replace(".txt", "-new.txt");
        File newFile = new File(pathDir, newName);

        BufferedReader reader = null;
        PrintWriter pw = null;

        try {
            reader = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(file)
                    )
            );
            pw = new PrintWriter(
                    new OutputStreamWriter(
                            new FileOutputStream(newFile)
                    )
            );
            String line = "";

            while ((line = reader.readLine()) != null) {
                if (line.startsWith("//")) {
                    continue;
                }
                pw.println();
            }
            pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
